//
//  LTXCustomAlertView.m
//  LazySchoolmate
//
//  Created by 李宗帅 on 2017/12/26.
//  Copyright © 2017年 李宗帅. All rights reserved.
//

#import "LTXCustomAlertView.h"
#define VersionAlertWidth 260
#define LeftSpace 18

static CGFloat animationDuration = 0.3f;

@interface LTXCustomAlertView ()

@property (strong, nonatomic) UILabel *titleLabel;

@property (strong, nonatomic) UILabel *textLabel;

@property (strong, nonatomic) UILabel *subTextLabel;

@property (nonatomic, strong) UIView *bottomline;

@property (nonatomic, strong) UIView *middleLine;

@property (copy, nonatomic) NSArray *otherButtons;

@property (strong, nonatomic) UIButton *cancelButton;

@property (strong, nonatomic) UIView *blackUnderView;

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UIImageView *descImageView;

@property (copy, nonatomic) void (^cancelButtonBlock)(void);

@property (copy, nonatomic) void (^othersButtonBlock)(NSInteger index);

@end
@implementation LTXCustomAlertView

- (LTXCustomAlertView *)initWithTitle:(NSString *)title text:(NSString *)text cancelButton:(NSString *)cancel otherButtons:(NSArray *)others
{
    if (self = [super initWithFrame:CGRectZero]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0f;
        self.clipsToBounds = YES;
        
        CGFloat width = VersionAlertWidth - LeftSpace*2;
        
        if (title) {
            self.titleLabel.text = title;
            [self addSubview:self.titleLabel];
            CGSize size = [title getUISize:self.titleLabel.font limitWidth:width];
            self.titleLabel.frame = CGRectMake(LeftSpace, 25, width, size.height);
        }
        
        if (text) {
            self.textLabel.text = text;
            self.textLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.textLabel];
        }
        CGSize size = [text getUISize:self.textLabel.font limitWidth:width];
        self.textLabel.frame = CGRectMake(LeftSpace, self.titleLabel.bottom + 25, width, size.height);
        self.bottomline.hidden = YES;
        if (cancel && cancel.length > 0) {
            [self addSubview:self.cancelButton];
            [self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateHighlighted];
            [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
        }
        self.cancelButton.frame = CGRectMake(LeftSpace, self.textLabel.bottom + 22, width, 40);
        
        UIButton *lastButton = self.cancelButton;
        if (others.count > 0) {
            if (cancel && cancel.length > 0) {
                for (NSInteger i = 0; i < others.count; i++) {
                    NSString *buttonTitle = [others objectAtIndex:i];
                    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(LeftSpace, self.textLabel.bottom + 22, width, 40)];
                    button.tag = i;
                    button.titleLabel.font = [UIFont systemFontOfSize:14];
                    button.backgroundColor = [UIColor whiteColor];
                    //                    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    //                    [button setBackgroundColor:[UIColor colorWithHexString:0xdbdbdb] forState:UIControlStateHighlighted];
                    [button setTitle:buttonTitle forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateHighlighted];
                    [button addTarget:self action:@selector(othersButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [self addSubview:button];
                    lastButton = button;
                }
//                [self addSubview:self.bottomline];
//                self.bottomline.frame = CGRectMake(0, self.textLabel.bottom + 22, VersionAlertWidth/2.0, 0.5);
                if (lastButton != self.cancelButton && lastButton.tag == 0) {
                    
//                    self.bottomline.hidden = NO;
                    CGFloat buttonWidth = width/2.0 - 10;
                    self.cancelButton.width = buttonWidth;
                    [self.cancelButton setTitleColor:[UIColor colorWithHexString:@"0xff5a3d"] forState:UIControlStateNormal];
                    self.cancelButton.backgroundColor = [UIColor whiteColor];
                    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage5_220x92"] forState:UIControlStateNormal];
                    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage5_220x92"] forState:UIControlStateHighlighted];
                    lastButton.width = buttonWidth;
                    lastButton.left = self.cancelButton.right + 10;
                    lastButton.top = self.cancelButton.top;
                }
            }else {
                for (NSInteger i = 0; i < others.count; i++) {
                    NSString *buttonTitle = [others objectAtIndex:i];
                    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, self.bottomline.bottom, VersionAlertWidth, 40)];
                    button.tag = i;
                    button.titleLabel.font = [UIFont systemFontOfSize:14];
                    //                    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    //                    [button setBackgroundColor:HexRGB(0xdbdbdb) forState:UIControlStateHighlighted];
                    button.backgroundColor = [UIColor whiteColor];
                    [button setTitle:buttonTitle forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    
                    [button addTarget:self action:@selector(othersButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [self addSubview:button];
                    lastButton = button;
                }
            }
        }
        
        self.frame = CGRectMake(0, 0, VersionAlertWidth, lastButton.bottom + 20);
    }
    return self;
}

- (LTXCustomAlertView *)initWithImage:(NSString *)image text:(NSString *)text cancelButton:(NSString *)cancel otherButtons:(NSArray *)others
{
    if (self = [super initWithFrame:CGRectZero]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0f;
        self.clipsToBounds = YES;
        
        CGFloat width = VersionAlertWidth - LeftSpace*2;
        
        if (image) {
            self.imageView.image = [UIImage imageNamed:image];
            [self addSubview:self.imageView];
            [self.imageView sizeToFit];
            CGSize size = self.imageView.size;
            self.imageView.frame = CGRectMake((VersionAlertWidth - size.width)/2, 25, size.width, size.height);
        }
        
        if (text) {
            self.textLabel.text = text;
            [self addSubview:self.textLabel];
        }
        CGSize size = [text getUISize:self.textLabel.font limitWidth:width];
        self.textLabel.frame = CGRectMake(LeftSpace, self.imageView.bottom + 25, width, size.height);
        self.bottomline.hidden = YES;
        if (cancel && cancel.length > 0) {
            [self addSubview:self.cancelButton];
            [self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateHighlighted];
            [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
        }
        self.cancelButton.frame = CGRectMake(0, self.textLabel.bottom + 22, VersionAlertWidth, 40);
        
        UIButton *lastButton = self.cancelButton;
        if (others.count > 0) {
            if (cancel && cancel.length > 0) {
                for (NSInteger i = 0; i < others.count; i++) {
                    NSString *buttonTitle = [others objectAtIndex:i];
                    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, self.textLabel.bottom + 22, width, 40)];
                    button.tag = i;
                    button.titleLabel.font = [UIFont systemFontOfSize:14];
                    button.backgroundColor = [UIColor whiteColor];
                    //                    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    //                    [button setBackgroundColor:[UIColor colorWithHexString:0xdbdbdb] forState:UIControlStateHighlighted];
                    [button setTitle:buttonTitle forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateHighlighted];
                    [button addTarget:self action:@selector(othersButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [self addSubview:button];
                    lastButton = button;
                }
                [self addSubview:self.bottomline];
                self.bottomline.frame = CGRectMake(0, self.textLabel.bottom + 22, VersionAlertWidth/2.0, 0.5);
                if (lastButton != self.cancelButton && lastButton.tag == 0) {
                    
                    self.bottomline.hidden = NO;
                    CGFloat buttonWidth = VersionAlertWidth/2.0;
                    self.cancelButton.width = buttonWidth;
                    [self.cancelButton setTitleColor:[UIColor colorWithHexString:@"0xff5a3d"] forState:UIControlStateNormal];
                    self.cancelButton.backgroundColor = [UIColor whiteColor];
                    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
                    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
                    lastButton.width = buttonWidth;
                    lastButton.left = self.cancelButton.right;
                    lastButton.top = self.cancelButton.top;
                }
            }else {
                for (NSInteger i = 0; i < others.count; i++) {
                    NSString *buttonTitle = [others objectAtIndex:i];
                    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, self.bottomline.bottom, VersionAlertWidth, 40)];
                    button.tag = i;
                    button.titleLabel.font = [UIFont systemFontOfSize:14];
                    //                    [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    //                    [button setBackgroundColor:HexRGB(0xdbdbdb) forState:UIControlStateHighlighted];
                    button.backgroundColor = [UIColor whiteColor];
                    [button setTitle:buttonTitle forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    
                    [button addTarget:self action:@selector(othersButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [self addSubview:button];
                    lastButton = button;
                }
            }
        }
        
        self.frame = CGRectMake(0, 0, VersionAlertWidth, lastButton.bottom);
    }
    return self;
}

- (LTXCustomAlertView *)initWithImage:(NSString *)image descImg:(NSString *)descImg text:(NSString *)text cancelButton:(NSString *)cancel
{
    if (self = [super initWithFrame:CGRectZero]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5.0f;
        self.clipsToBounds = YES;
        
        CGFloat width = VersionAlertWidth - LeftSpace*2;
        
        if (image) {
            self.imageView.image = [UIImage imageNamed:image];
            [self addSubview:self.imageView];
            [self.imageView sizeToFit];
            CGSize size = self.imageView.size;
            self.imageView.frame = CGRectMake((VersionAlertWidth - size.width)/2, 25, size.width, size.height);
        }
        
        if (descImg) {
            self.descImageView.image = [UIImage imageNamed:descImg];
            [self addSubview:self.descImageView];
            [self.descImageView sizeToFit];
            CGSize size = self.descImageView.size;
            self.descImageView.frame = CGRectMake((VersionAlertWidth - size.width)/2, self.imageView.bottom + 25, size.width, size.height);
        }
        
        if (text) {
            self.textLabel.text = text;
            self.textLabel.font = [UIFont systemFontOfSize:12];
            [self addSubview:self.textLabel];
        }
        CGSize size = [text getUISize:self.textLabel.font limitWidth:width];
        self.textLabel.frame = CGRectMake(LeftSpace, descImg.length?self.descImageView.bottom:self.imageView.bottom + 25, width, size.height);
        self.bottomline.hidden = YES;
        if (cancel && cancel.length > 0) {
            [self addSubview:self.cancelButton];
            [self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateNormal];
            [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"button_guidepage2_480x92"] forState:UIControlStateHighlighted];
            [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
        }
        self.cancelButton.frame = CGRectMake(LeftSpace, self.textLabel.bottom + 22, width , 40);
        
        UIButton *lastButton = self.cancelButton;
        
        self.frame = CGRectMake(0, 0, VersionAlertWidth, lastButton.bottom + 20);
    }
    return self;
}

- (void)setCancelButtonBlock:(void (^)(void))cancelBlocl dismissBlock:(void (^)(NSInteger))dismissBlock
{
    self.cancelButtonBlock = cancelBlocl;
    self.othersButtonBlock = dismissBlock;
}

- (void)showVersionAlertView
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window viewWithTag:20150101];
    UIView *view1 = [window viewWithTag:20150102];
    if (view || view1) {
        [view removeFromSuperview];
        [view1 removeFromSuperview];
    }
    self.blackUnderView.tag = 20150101;
    self.tag = 20150102;
    [window addSubview:self.blackUnderView];
    [window addSubview:self];
    self.center = window.center;
    self.transform = CGAffineTransformMakeScale(1.02, 1.02);
    @weakify(self);
    [UIView animateWithDuration:animationDuration animations:^{
        @strongify(self);
        self.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissVersionAlertView
{
    @weakify(self);
    [UIView animateWithDuration:animationDuration animations:^{
        @strongify(self);
        self.blackUnderView.hidden = YES;
        self.hidden = YES;
    } completion:^(BOOL finished) {
        @strongify(self);
        [self.blackUnderView removeFromSuperview];
        [self removeFromSuperview];
    }];
    
}

#pragma mark - Button Events handle
- (void)cancelButtonOnClicked
{
    if (self.cancelButtonBlock) {
        self.cancelButtonBlock();
    }
    [self dismissVersionAlertView];
}

- (void)othersButtonOnClicked:(UIButton *)button
{
    if (self.othersButtonBlock) {
        self.othersButtonBlock(button.tag);
    }
    [self dismissVersionAlertView];
}


#pragma mark - Getter Methods
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:16];
        label.textAlignment = NSTextAlignmentCenter;
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    }
    return _imageView;
}

- (UIImageView *)descImageView
{
    if (!_descImageView) {
        _descImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
    }
    
    return _descImageView;
}

- (UILabel *)textLabel
{
    if (!_textLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithHexString:@"0x333333"];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentLeft;
        _textLabel = label;
    }
    return _textLabel;
}

- (UILabel *)subTextLabel
{
    if (!_subTextLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithHexString:@"0x333333"];;
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        _subTextLabel = label;
    }
    return _subTextLabel;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.backgroundColor = [UIColor whiteColor];
        //        [button setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //        [button setBackgroundColor:[UIColor graylineColor] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor colorWithHexString:@"0x0076ff"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(cancelButtonOnClicked) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton = button;
    }
    return _cancelButton;
}

- (UIView *)blackUnderView
{
    if (!_blackUnderView) {
        _blackUnderView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _blackUnderView.backgroundColor = [UIColor blackColor];
        _blackUnderView.alpha = 0.4;
    }
    return _blackUnderView;
}

- (UIView *)bottomline
{
    if (_bottomline) {
        return _bottomline;
    }
    _bottomline = [[UIView alloc] initWithFrame:CGRectZero];
    _bottomline.backgroundColor = [UIColor colorWithHexString:@"0xcccccc"];
    _bottomline.hidden = YES;
    return _bottomline;
}

- (UIView *)middleLine
{
    if (_middleLine) {
        return _middleLine;
    }
    _middleLine = [[UIView alloc] initWithFrame:CGRectZero];
    _middleLine.backgroundColor = [UIColor grayColor];
    return _middleLine;
}

@end

